<?php
class omepkg_ome_product{
    public function checkProductByBn($bn){
        $sql = "SELECT goods_id FROM `sdb_omepkg_pkg_goods` WHERE `pkg_bn`='".trim($bn)."' ";
        $gondsInfo = kernel::database()->selectrow($sql);

        if(empty($gondsInfo)){
            return false;
        }
        return $gondsInfo['goods_id'];
    }
    
    public function getProductByBn($bn){
        $sql = "SELECT goods_id as product_id,pkg_bn as bn,name FROM `sdb_omepkg_pkg_goods` WHERE `pkg_bn`='".trim($bn)."' ";
        $gondsInfo = kernel::database()->selectrow($sql);

        if(empty($gondsInfo)){
            return false;
        }
        return $gondsInfo;
    }
    
    public function getProductInfoByBn($bn){
        $goodsObj   = &app::get('omepkg')->model('pkg_goods');
        $productObj = &app::get('omepkg')->model('pkg_product');
        
        $goodsInfo = $goodsObj->dump(array('pkg_bn'=>$bn),"goods_id,pkg_bn as bn,name,weight");
        if (!$goodsInfo) return false;
        
        $products = $productObj->getList('*',array('goods_id'=>$goodsInfo['goods_id']),0,-1);
        if ($products){
            $items = array();
            foreach ($products as $val){
                $val['nums'] = $val['pkgnum'];
                $items[] = $val;
            }
            $goodsInfo['items'] = $items;
        }
        $goodsInfo['product_id']    = $goodsInfo['goods_id'];
        $goodsInfo['product_type']  = 'pkg';
        $goodsInfo['product_desc']  = '捆绑商品';
        
        return $goodsInfo;
    }
    
// public function ee(){ $sql_pkg="select pkg_bn from sdb_omepkg_pkg_goods";
//             	$pkgbn_list = kernel::database()->select($sql_pkg);
//             	foreach($pkgbn_list as $k=>$v){
//             		$pkgbn[]=$v['pkg_bn'];            	
//             	}
// }
    /**
     * 取得捆绑商品库存
     *
     */
    public function getPKGStore($productids){
    	$stocks=array();
    	$quantity_pkg=array();
    
    
    	$pkgpSql="SELECT pkg_bn,g.goods_id,product_id,pkgnum  FROM `sdb_omepkg_pkg_product` p
    	left join  `sdb_omepkg_pkg_goods` g on p.goods_id=g.goods_id
    	where product_id in ($productids) ";
    	$pkgpbn_list = kernel::database()->select($pkgpSql);
    	if($pkgpbn_list){
    
    		foreach($pkgpbn_list as $k=>$v){
    			//反查捆绑商品库存
    			$pkg_bn=$v['pkg_bn'];
    				
    			//取得sku_id
    			$sql_sku="select addon,sku,node_id from sdb_ome_product_sku_map where bn=".$v['pkg_bn'];
    			$skus = kernel::database()->selectrow($sql_sku);
    			$skuArr=array();
    			foreach ($skus as $sk=>$sv){
    				$skuArr[$sv['node_id']]=array($sv['sku'],$sv['addon']);
    			}
    			
    			$sql = " SELECT sum(bp.store) as store,sum(bp.store_freeze) as store_freeze FROM `sdb_ome_branch_product` bp,`sdb_ome_branch` b
    			WHERE bp.`product_id`='".$v['product_id']."' and bp.`branch_id`=b.`branch_id` AND b.`attr`='true' ";
    			$total_store = kernel::database()->selectrow($sql);
    				
    			//单品库存
    			//回写库存值 = 线上仓库库存总和 - 预占库存
    			$quantity = $total_store['store'] - $total_store['store_freeze'];
    				
    			//捆绑库存
    			$quantity_pkg[$pkg_bn]['num'][] = floor($quantity/$v['pkgnum']);//向下取整
    			$quantity_pkg[$pkg_bn]['skus'] = $skuArr;//向下取整
    			//$quantity_pkg[$pkg_bn][] = array('pkgquantity'=>floor($quantity/$v['pkgnum']),'gome_sku_id'=>$v['gome_sku_id']);//向下取整
    				
    		}
    
    		foreach($quantity_pkg as $pk=>$pv){
    			$stocks[] = array(
    					'bn' => trim($pk),
    					'quantity' => min($pv['num']),
    					'skus' => $pv['skus'],
    			);
    		}
    	}
    
    	return $stocks;
    
    }
    
}
